#include <iostream>
#include <iterator>
#include <list>
#include <vector>

int main()
{
    { // 基本遍历操作
        std::vector<int> nVec = {1, 2, 3, 4, 5};

        // 使用迭代器遍历
        for (std::vector<int>::iterator it = nVec.begin(); it != nVec.end(); ++it)
        {
            std::cout << *it << " ";
        }
        std::cout << std::endl;

        // 使用 auto 简化
        for (auto it = nVec.begin(); it != nVec.end(); ++it)
        {
            std::cout << *it << " ";
        }
        std::cout << std::endl;

        // 使用常量迭代器
        for (std::vector<int>::const_iterator it = nVec.cbegin(); it != nVec.cend(); ++it)
        {
            std::cout << *it << " ";
        }
        std::cout << std::endl;

        // List 迭代器示例 (双向迭代器)
        std::list<std::string> strList = {"Hello", "World", "C++"};

        // 正向遍历
        for (auto it = strList.begin(); it != strList.end(); ++it)
        {
            std::cout << *it << " ";
        }
        std::cout << std::endl;

        // 反向遍历
        for (auto rit = strList.rbegin(); rit != strList.rend(); ++rit)
        {
            std::cout << *rit << " ";
        }
        std::cout << std::endl;
    }

    { // 迭代器操作函数
        std::vector<int> nVec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        // 使用 std::advance
        auto it = nVec.begin();
        std::advance(it, 5); // 前进5个位置
        std::cout << "After advance: " << *it << std::endl; // 输出6

        // 使用 std::distance
        auto dist = std::distance(nVec.begin(), it);
        std::cout << "Distance: " << dist << std::endl; // 输出5

        // 使用 std::next 和 std::prev
        auto nextIt = std::next(it, 2); // it之后第2个位置
        auto prevIt = std::prev(it, 3); // it之前第3个位置
        std::cout << "Next: " << *nextIt << ", Prev: " << *prevIt << std::endl;

        // 使用 std::begin 和 std::end (非成员函数)
        auto start = std::begin(nVec);
        auto finish = std::end(nVec);
        std::cout << "First: " << *start << ", Size: " << std::distance(start, finish) << std::endl;
    }

    { // 迭代器适配器
        std::vector<int> nVec1 = {1, 2, 3, 4, 5};
        std::vector<int> nVec2{};

        // 使用 back_inserter
        std::copy(nVec1.begin(), nVec1.end(), std::back_inserter(nVec2));
        std::cout << "After back_inserter: ";
        for (auto n : nVec2)
        {
            std::cout << n << " ";
        }
        std::cout << std::endl;

        // 使用 front_inserter (需要支持push_front的容器)
        std::list<int> list_target;
        std::copy(nVec1.begin(), nVec1.end(), std::front_inserter(list_target));
        std::cout << "After front_inserter: ";
        for (auto n : list_target)
        {
            std::cout << n << " ";
        }
        std::cout << std::endl;

        // 使用 inserter
        std::vector<int> vec_target = {10, 20, 30};
        auto it = vec_target.begin() + 1; // 指向20
        std::copy(nVec1.begin(), nVec1.end(), std::inserter(vec_target, it));
        std::cout << "After inserter: ";
        for (auto n : vec_target)
        {
            std::cout << n << " ";
        }
        std::cout << std::endl;

        // 2. 流迭代器
        std::cout << "Enter numbers (Ctrl+D to end): ";
        std::istream_iterator<int> input_it(std::cin);
        std::istream_iterator<int> end_it;

        std::vector<int> input_numbers;
        std::copy(input_it, end_it, std::back_inserter(input_numbers));

        std::cout << "You entered: ";
        std::ostream_iterator<int> output_it(std::cout, " ");
        std::copy(input_numbers.begin(), input_numbers.end(), output_it);
        std::cout << std::endl;

    }
    return 0;
}
